﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGWindowApp.Panels;
using IGGCore;
using IGGWindowApp.Data;

namespace IGGWindowApp
{
    public partial class MainFormBeta : Form
    {

        // ENUM
        public enum ZONES
        {
            NORTH, SOUTH, EAST, WEST, CENTRAL
        }

        public enum UIACTIONS
        {
            INITSCOREBOARD, INITLOG, INITNEWS, INITRANS, INITTWITTER, INITHEADER, INITSTATUS, INITSTADIUM, INITNAVIGATOR,
            INITINGAME, INITSTARS, INITWEB, INITSTANDINGS, INITSTATS,
            SWITCHLOADING,
            SWITCHLEFT, SWITCHCENTRAL, SWITCHRIGHT, SWITCHUP, SWITCHDOWN,
            TITLE, ADDLOG, NAVIGATE, 
            UPDATENEWS, UPDATETWITTER, UPDATETRANS, UPDATEHEADER, UPDATETEAM, UPDATESTADIUM, UPDATESTANDINGS,
            UPDATESCOREBOARD, UPDATETICKER, UPDATENAVIGATOR, UPDATEINGAME, UPDATEPICS, UPDATESTARS
        }

        // DELEGATES
        public delegate void DelegateUpdateUI(UIACTIONS action, object[] parameters);
        public DelegateUpdateUI myUpdateUI;

        // EVENTS
        public delegate void URLClickedHandler(string url);
        public delegate void TeamRequestHandler(string team);
        public delegate void GameSelectedHandler(Game game);
        public event GameSelectedHandler GameSelected;
        public event EventHandler MainFormLoaded;
        public event EventHandler MainFormClosed;
        public event URLClickedHandler URLClicked;
        public event EventHandler DebugStarted;
        public TeamRequestHandler TeamRequested;

        // PANELS
        private PanelScoreboard m_panelScoreboard;
        private PanelLog m_panelLog;
        private PanelNews m_panelNews;
        private PanelTwitter m_panelTwitter;
        private PanelTransactions m_panelTrans;
        private WebPanel m_panelWeb;
        private PanelHeader m_panelHeader;
        private PanelTeam m_panelTeam;
        private PanelStatus m_panelStatus;
        private PanelStadium m_panelStadium;
        private PanelGameNavigator m_panelGameNavigator;
        private PanelInGame m_panelInGame;
        private PanelStars m_panelStars;
        private PanelStandings m_panelStandings;
        private PanelStatistics m_panelStats;

        /// <summary>
        /// Main Constructor - delegates initialization
        /// </summary>
        public MainFormBeta()
        {
            InitializeComponent();
            myUpdateUI = new DelegateUpdateUI(this.UpdateUI);
        }

        #region events handling

        /// <summary>
        /// Override the onLoad event manager
        /// </summary>
        protected override void OnLoad(EventArgs args)
        {
            base.OnLoad(args);
            Application.Idle += new EventHandler(OnLoaded);
        }

        /// <summary>
        /// Manage the OnLoaded event.
        /// Throws the MainFormLoaded event for the manager
        /// </summary>
        private void OnLoaded(object sender, EventArgs args)
        {
            Application.Idle -= new EventHandler(OnLoaded);
            MainFormLoaded(this, null);
        }

        private void MainFormBeta_Resize(object sender, EventArgs e)
        {
            // RESIZING!
        }

        private void MainFormBeta_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainFormClosed(this, null);
        }

        private void ItemSelected(string item)
        {
            UpdateUI(UIACTIONS.SWITCHLEFT, new object[] { item.ToUpper() });
        }

        private void ItemCentralSelected(string item)
        {
            UpdateUI(UIACTIONS.SWITCHCENTRAL, new object[] { item.ToUpper() });
        }

        private void m_panelGameNavigator_GameSelected(Game game)
        {
            GameSelected(game);
        }

        private void m_panelTrans_NextPanelClicked(object sender, EventArgs e)
        {
            m_panelLog.Visible = true;
            m_panelTrans.Visible = false;
        }

        private void m_panelLog_NextPanelClicked(object sender, EventArgs e)
        {
            m_panelTrans.Visible = true;
            m_panelLog.Visible = false;
        }

        private void m_panelNews_NextPanelClicked(object sender, EventArgs e)
        {
            //m_panelWeb.Visible = true;
            m_panelNews.Visible = false;
        }

        private void m_panelTwitter_NextPanelClicked(object sender, EventArgs e)
        {
            if (m_panelGameNavigator != null)
            {
                m_panelGameNavigator.Visible = false;
            }
            TeamRequested(null);
        }

        private void m_panelTeam_NextPanelClicked(object sender, EventArgs e)
        {
            if (m_panelGameNavigator != null)
            {
                m_panelGameNavigator.Visible = true;
            }
            else
            {
                m_panelTwitter.Visible = true;
            }
            m_panelTeam.Visible = false;

        }

        private void m_panelStatus_DebugStarted(object sender, EventArgs e)
        {
            DebugStarted(this, null);
        }

        /// <summary>
        /// Manage a link clicked. Time to load the web browser.
        /// </summary>
        private void LinkClicked(object sender, LinkClickedEventArgs e)
        {
            if (m_panelWeb == null)
            {
                m_panelWeb = new WebPanel();
                m_panelWeb.Dock = DockStyle.Fill;
                tabPageWeb.Controls.Add(m_panelWeb);
            }
            m_panelWeb.LoadURL(e.LinkText);
            this.tabControlCentral.SelectedTab = tabPageWeb;
        }

        #endregion


        /// <summary>
        /// Main Update UI method
        /// </summary>
        private void UpdateUI(UIACTIONS action, object[] parameters)
        {
            switch (action)
            {
                case UIACTIONS.SWITCHLEFT:
                    if (parameters[0].ToString() == "TWITTER")
                    {
                        tabControlLeft.SelectedTab = tabPageTwitter;
                    }
                    break;
                case UIACTIONS.SWITCHUP:
                    if (parameters[0].ToString() == "SCOREBOARD")
                    {
                        m_panelScoreboard.Visible = true;
                        m_panelHeader.Visible = false;
                        
                    }
                    else if (parameters[0].ToString() == "HEADER")
                    {
                        m_panelHeader.Visible = true;
                        m_panelScoreboard.Visible = false;
                    }
                    break;
                case UIACTIONS.SWITCHDOWN:
                    m_panelStatus.Visible = true;
                    break;
                case UIACTIONS.SWITCHCENTRAL:
                    if (parameters[0].ToString() == "STADIUM")
                    {
                        tabControlCentral.SelectedTab = tabPageStadium;
                    }
                    break;
                case UIACTIONS.TITLE:
                    this.Text = parameters[0].ToString();
                    break;
                case UIACTIONS.INITSCOREBOARD:
                    m_panelScoreboard = new PanelScoreboard();
                    m_panelScoreboard.Dock = DockStyle.Fill;
                    m_panelScoreboard.Visible = false;
                    panelTop.Controls.Add(m_panelScoreboard);
                    panelTop.Refresh();
                    break;
                case UIACTIONS.INITHEADER:
                    m_panelHeader = new PanelHeader();
                    m_panelHeader.Dock = DockStyle.Fill;
                    m_panelHeader.Visible = true;
                    panelTop.Controls.Add(m_panelHeader);
                    panelTop.Refresh();
                    break;
                case UIACTIONS.INITLOG:
                    m_panelLog = new PanelLog();
                    m_panelLog.Dock = DockStyle.Fill;
                    m_panelLog.Visible = true;
                    tabPageLog.Controls.Add(m_panelLog);
                    tabPageLog.Refresh();
                    break;
                case UIACTIONS.INITSTARS:
                    m_panelStars = new PanelStars();
                    m_panelStars.Dock = DockStyle.Fill;
                    m_panelStars.Visible = true;
                    tabPageStars.Controls.Add(m_panelStars);
                    break;
                case UIACTIONS.INITINGAME:
                    m_panelInGame = new PanelInGame();
                    m_panelInGame.Dock = DockStyle.Fill;
                    panelLeft.Controls.Add(m_panelInGame);
                    m_panelInGame.ItemSelected += new PanelInGame.ItemSelectedHandler(ItemSelected);
                    m_panelInGame.Visible = false;
                    break;
                case UIACTIONS.INITNEWS:
                    m_panelNews = new PanelNews();
                    m_panelNews.Dock = DockStyle.Fill;
                    tabPageNews.Controls.Add(m_panelNews);
                    tabPageNews.Refresh();
                    break;
                case UIACTIONS.INITSTATS:
                    m_panelStats = new PanelStatistics();
                    m_panelStats.Dock = DockStyle.Fill;
                    tabPageStats.Controls.Add(m_panelStats);
                    tabPageStats.Refresh();
                    break;
                case UIACTIONS.INITSTANDINGS:
                    m_panelStandings = new PanelStandings();
                    m_panelStandings.Dock = DockStyle.Fill;
                    tabPageStandings.Controls.Add(m_panelStandings);
                    tabPageStandings.Refresh();
                    break;
                case UIACTIONS.INITTWITTER:
                    m_panelTwitter = new PanelTwitter();
                    m_panelTwitter.URLClicked += new System.Windows.Forms.LinkClickedEventHandler(this.LinkClicked);
                    m_panelTwitter.Dock = DockStyle.Fill;
                    tabPageTwitter.Controls.Add(m_panelTwitter);
                    break;
                case UIACTIONS.INITRANS:
                    m_panelTrans = new PanelTransactions();
                    m_panelTrans.ItemSelected += new PanelTransactions.ItemSelectedHandler(ItemSelected);
                    m_panelTrans.Dock = DockStyle.Fill;
                    tabPageTrans.Controls.Add(m_panelTrans);
                    tabPageTrans.Refresh();
                    break;
                case UIACTIONS.INITWEB:
                    m_panelWeb = new WebPanel();
                    m_panelWeb.Dock = DockStyle.Fill;
                    m_panelWeb.Visible = false;
                    panelMain.Controls.Add(m_panelWeb);
                    break;
                case UIACTIONS.INITSTATUS:
                    m_panelStatus = new PanelStatus();
                    m_panelStatus.Dock = DockStyle.Fill;
                    m_panelStatus.DebugStarted += new EventHandler(m_panelStatus_DebugStarted);
                    panelBottom.Controls.Add(m_panelStatus);
                    panelBottom.Refresh();
                    break;
                case UIACTIONS.INITNAVIGATOR:
                    m_panelGameNavigator = new PanelGameNavigator();
                    m_panelGameNavigator.GameSelected += new PanelGameNavigator.GameSelectedHandler(m_panelGameNavigator_GameSelected);
                    m_panelGameNavigator.Dock = DockStyle.Fill;
                    m_panelGameNavigator.Visible = true;
                    tabPageGames.Controls.Add(m_panelGameNavigator);
                    tabPageGames.Refresh();
                    break;
                case UIACTIONS.INITSTADIUM:
                    m_panelStadium = new PanelStadium();
                    m_panelStadium.Dock = DockStyle.Fill;
                    m_panelStadium.Visible = true;
                    tabPageStadium.Controls.Add(m_panelStadium);
                    tabPageStadium.Refresh();
                    break;
                case UIACTIONS.UPDATENEWS:
                    m_panelNews.SetData(parameters[0] as List<News>);
                    break;
                case UIACTIONS.UPDATETWITTER:
                    m_panelTwitter.SetData(parameters[0] as Tweet[]);
                    break;
                case UIACTIONS.NAVIGATE:
                    m_panelWeb.LoadURL(parameters[0].ToString());
                    break;
                case UIACTIONS.UPDATETRANS:
                    m_panelTrans.SetData(parameters[0] as List<TransactionDay>);
                    break;
                case UIACTIONS.UPDATEHEADER:
                    m_panelHeader.SetData(parameters[0] as News);
                    break;
                case UIACTIONS.UPDATETEAM:
                    if (m_panelTeam == null)
                    {
                        m_panelTeam = new PanelTeam();
                        m_panelTeam.NextPanelClicked += new EventHandler(m_panelTeam_NextPanelClicked);
                        m_panelTeam.Dock = DockStyle.Fill;
                        panelRight.Controls.Add(m_panelTeam);
                        m_panelTwitter.Visible = false;
                    }
                    else
                    {
                        m_panelTeam.Visible = true;
                        m_panelTwitter.Visible = false;
                    }
                    m_panelTeam.SetData(parameters[0] as Team);
                    panelMain.Refresh();
                    break;
                case UIACTIONS.UPDATESTADIUM:
                    m_panelStadium.SetData(parameters[0] as WindowStadium, parameters[1] as Game);
                    break;
                case UIACTIONS.UPDATESCOREBOARD:
                    m_panelScoreboard.SetData(parameters[0] as Game);
                    break;
                case UIACTIONS.UPDATEINGAME:
                    //m_panelInGame.SetData(parameters[0] as Game, parameters[1].ToString(), (Player[])parameters[2]);
                    break;
                case UIACTIONS.UPDATEPICS:
                    //m_panelInGame.SetPics(parameters[0] as Image, parameters[1] as Image);
                    break;
                case UIACTIONS.UPDATETICKER:
                    m_panelStatus.SetData(parameters[0].ToString());
                    break;
                case UIACTIONS.UPDATESTANDINGS:
                    m_panelStandings.SetData(parameters[0] as Scoreboard);
                    break;
                case UIACTIONS.UPDATENAVIGATOR:
                    m_panelGameNavigator.SetData(parameters[0] as Scoreboard);
                    break;
                case UIACTIONS.UPDATESTARS:
                    m_panelStars.UpdateStars((Player[])parameters[0]);
                    break;
                case UIACTIONS.ADDLOG:
                    if (m_panelLog != null)
                    {
                        m_panelLog.AddLog(parameters[0].ToString());
                    }
                    break;
            }
        }













    }
}
